package com.base.data.elasticsearch.util;

import com.google.common.base.Strings;
import lombok.NonNull;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;

public class BaseDateUtil extends DateUtils {

    private static final Logger logger = LoggerFactory.getLogger(BaseDateUtil.class.getName());
    public static final String DEFAULT_TIME_PATTER = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_PATTER_YYYYMMDD = "yyyy-MM-dd";
    public static final String TIME_PATTER_HHMMSS = "HH:mm:ss";
    private static String[] defaultPattern = new String[] {DEFAULT_TIME_PATTER,TIME_PATTER_YYYYMMDD,TIME_PATTER_HHMMSS};

    public static final String CHINA_DATE_PATTER = "yyyy年MM月dd日";
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
    public static final String DATE_FORMAT_FOR_RECORD = "yyyyMMdd";
    public static final String DATE_FORMAT_FOR_NAME = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_WITH_VIRGULE = "dd/MM/yyyy";
    public static final String HOUR_FORMAT_WITH_WIRGULE = "dd/MM/yyyy HH:mm";
    public static final String SHORT_FROMAT_WITH_HOUR_MINUTE = "HH:mm";
    public static final String LOCAL_DATE_FORMAT_WITH_VIRGULE = "yyyy/MM/dd";
    public static final String LOCAL_HOUR_FORMAT_WITH_WIRGULE = "yyyy/MM/dd HH:mm";
    public static final String LOCAL_MONTH_FORMAT_WITH_WIRGULE = "yyyy/MM";


    public static String transtfLocalDatetime2String(LocalDateTime localDateTime){
        return transtfLocalDatetime2String(localDateTime,DEFAULT_TIME_PATTER);
    }

    public static String transtfLocalDatetime2String(LocalDateTime localDateTime,String patten){
        return localDateTime.format(DateTimeFormatter.ofPattern(patten));
    }

    public static Date parseDate(String source) {
        return parseDateWithPattern(source,DEFAULT_TIME_PATTER);
    }

    public static Date parseDateWithPattern(String source,String pattern) {
        if (source == null) {
            return null;
        }
        if (!Strings.isNullOrEmpty(pattern) && !Arrays.stream(defaultPattern).filter(p ->p.equals(pattern)).findAny().isPresent()) {
            String[] defaultPattern1 = new String[defaultPattern.length+1];
            defaultPattern1[0]=pattern;
            for (int i = 0; i < defaultPattern.length; i++) {
                defaultPattern1[i+1]=defaultPattern[i];
            }
            defaultPattern = defaultPattern1;
        }

        Date result = null;
        try {
            result = parseDate(source,defaultPattern);
        } catch (ParseException ex) {
            logger.error("Date format must be " + pattern);
        }
        return result;
    }

    public static String format(@NonNull Date date, @NonNull String pattern) {
        FastDateFormat format = FastDateFormat.getInstance(pattern);
        return format.format(date);
    }

    public static String format(@NonNull Date date) {
        return format(date,DEFAULT_TIME_PATTER);
    }
}
